import { DatePicker } from '@/components/AntdProxy';
import Crud from '@/components/Crud';
import {
  ChargeStationSelect,
  DriverSelect,
  GroupTreeSelect,
  VehicleSelect,
} from '@/components/CustomForm';
import { PLATE_COLOR_MAP } from '@/constants/enums';
import { formatSeconds } from '@/utils/format';
import request from '@/utils/request';
import { DownloadOutlined } from '@ant-design/icons';
import { Button } from 'antd';
import dayjs from 'dayjs';
import { useRef } from 'react';

const getList = async (params) => {
  const res = await request({
    url: '/datas/inout/charge/station/list',
    method: 'GET',
    custom: true,
    params: params,
  });
  return {
    total: res?.total,
    data: res?.data,
  };
};

const Index = () => {
  const crudRef = useRef();

  const columns = [
    {
      title: '序号',
      fixed: 'left',
      width: 65,
      render: (text, record, index) => index + 1,
    },
    {
      title: '组织',
      dataIndex: 'groupName',
      fixed: 'left',
      width: 220,
    },
    {
      title: '充电站',
      dataIndex: 'chargeStationName',
      fixed: 'left',
      width: 180,
    },
    {
      title: '车牌号',
      dataIndex: 'license',
      fixed: 'left',
      width: 120,
    },
    {
      title: '车牌颜色',
      dataIndex: 'plateColor',
      width: 90,
      render: (text) => PLATE_COLOR_MAP[text] || text,
    },
    {
      title: '驾驶员',
      dataIndex: 'realname',
      width: 100,
    },
    {
      title: '进入时间',
      dataIndex: 'inTime',
      width: 180,
    },
    {
      title: '离开时间',
      dataIndex: 'outTime',
      width: 180,
    },
    {
      title: '总时长',
      dataIndex: 'duration',
      width: 100,
      render: (text) => formatSeconds(text),
    },
    {
      title: '系统时间',
      dataIndex: 'time',
      width: 180,
    },
    {
      title: '备注',
      dataIndex: 'remark',
      width: 150,
    },
  ];

  const fields = [
    {
      label: '组织',
      name: 'groupId',
      children: <GroupTreeSelect placeholder="请选择组织" />,
    },
    {
      label: '充电站',
      name: 'chargeStationId',
      children: <ChargeStationSelect placeholder="请选择充电站" />,
    },
    {
      label: '车牌号',
      name: 'vehicleId',
      children: <VehicleSelect placeholder="请选择车牌号" />,
    },
    {
      label: '驾驶员',
      name: 'driverId',
      children: <DriverSelect placeholder="请选择驾驶员" />,
    },
    {
      label: '开始时间',
      name: 'fromTime',
      children: (
        <DatePicker
          allowClear={false}
          showTime
          placeholder="请选择开始时间"
          format="YYYY-MM-DD HH:mm:ss"
        />
      ),
    },
    {
      label: '结束时间',
      name: 'toTime',
      children: (
        <DatePicker
          allowClear={false}
          showTime
          placeholder="请选择结束时间"
          format="YYYY-MM-DD HH:mm:ss"
        />
      ),
    },
  ];

  const handleExport = async () => {
    const params = crudRef.current?.getParams();
    const res = await request({
      url: '/datas/inout/charge/station/export/list',
      method: 'GET',
      params,
    });
    window.open(res);
  };

  return (
    <>
      <Crud
        ref={crudRef}
        rowKey="ioChargeStationId"
        queryFormProps={{
          labelCol: 7,
          initialValues: {
            fromTime: dayjs().startOf('day'),
            toTime: dayjs().endOf('day'),
          },
        }}
        fields={fields}
        columns={columns}
        request={getList}
        transformParams={(params) => {
          return {
            ...params,
            fromTime: params?.fromTime?.format('YYYY-MM-DD HH:mm:ss'),
            toTime: params?.toTime?.format('YYYY-MM-DD HH:mm:ss'),
          };
        }}
        toolbar={
          <>
            <Button
              type="primary"
              icon={<DownloadOutlined />}
              onClick={handleExport}
            >
              导出
            </Button>
          </>
        }
      />
    </>
  );
};

export default Index;
